//
//  CTDiscoveryViewController.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/29.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTDiscoveryViewController.h"
#import "CTPickImageViewController.h"
#import "CTTabBarController.h"
#import "CTCommentViewController.h"
#import "CTLoginViewController.h"
#import "CTUserViewController.h"

//config
#import "CTHeader.h"

//view
#import "CTDiscoveryView.h"


//logic
#import "CTDiscoveryLogic.h"

//model
#import "CTDiscoveryItems.h"
#import "CTShareItem.h"
#import "CTLogicItems.h"

//function
#import "CTActivitorView.h"
#import "CTDebugManager.h"
#import "CTAppLanguage.h"
#import "CTLogin.h"
#import "CTDeviceFunction.h"
#import "CTGenRequestData.h"
#import "CTDataGenerator.h"
#import "CTShare.h"
#import "CTShareView.h"
#import "CTRequestUtil.h"
#import "CTRemindView.h"
#import "CTLogic.h"
#import "CTRequestUtil.h"
#import "CTMoreView.h"
#import "CTActivitorView.h"
#import "CTTextManager.h"
#import "CTImageProcessing.h"
#import "MobClick.h"

//tool
#import "UIImageView+MJWebCache.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"

static NSInteger discoveryFlipNumber = 0;

typedef enum{
    ReportTypeReport = 1,
    ReportTypeDelete
}ReportType;


@interface CTDiscoveryViewController ()<CTDiscoveryViewDelegate,CTShareViewDelegate,CTMoreViewDelegate,UIAlertViewDelegate>
{
    CTDiscoveryView * discoveryView;
    
    CTDiscoveryLogic * discoveryLogic;
    
    CTShareView * shareView;
    ReportType reportType;
    
    CTFollowList * currentFollowListItem;
    CTActivitorView * acView;
    CTLogic * logic;
    UIImage     *shareImage;
}
@end

@implementation CTDiscoveryViewController

- (void)dealloc
{
    [acView removeFromSuperview];
    acView = nil;
    shareImage = nil;
    [discoveryView removeFromSuperview];
    discoveryView = nil;
    discoveryView.delegate = nil;
    discoveryLogic = nil;
    
    logic = nil;
    
    [shareView removeFromSuperview];
    shareView = nil;
    
    currentFollowListItem = nil;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CTLoginSuccessNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CTExitSuccessNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_FOLLOWING_SUCCESS object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_CANCEL_FOLLOWING_SUCCESS object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_FOLLOWING_Failed object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_LIKE_SUCCESS object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_CANCEL_LIKE_SUCCESS object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_PUBLISH_COMMENT_SUCCESS object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_DELETE_COMMENT_SUCCESS object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_DELETE_PUBLISH_SUCCESS object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_PUBLISH_SUCCESS object:nil];
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTDiscoveryViewController Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        discoveryLogic = [[CTDiscoveryLogic alloc] init];
        logic = [[CTLogic alloc] init];
        
        //注册登录&退出
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess:) name:CTLoginSuccessNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitSuccess:) name:CTExitSuccessNotification object:nil];
        
        //关注成功，从新刷新数据库数据
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(followPersonSuccess:) name:CT_NOTI_FOLLOWING_SUCCESS object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancelFollowPersonSuccess:) name:CT_NOTI_CANCEL_FOLLOWING_SUCCESS object:nil];
        //关注失败
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(followPersonFailed:) name:CT_NOTI_FOLLOWING_Failed object:nil];
        
        //点赞成功，从新刷新数据库数据
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(likePersonSuccess:) name:CT_NOTI_LIKE_SUCCESS object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancelLikePersonSuccess:) name:CT_NOTI_CANCEL_LIKE_SUCCESS object:nil];
        
        //评论成功
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(publishCommentSuccessed:) name:CT_NOTI_PUBLISH_COMMENT_SUCCESS object:nil];
        
        //删除评论
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteCommentSuccessed:) name:CT_NOTI_DELETE_COMMENT_SUCCESS object:nil];
        
        //删除发布内容
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deletePublishSuccessed:) name:CT_NOTI_DELETE_PUBLISH_SUCCESS object:nil];
        
        
        //注册发布内容通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(publishSuccess:) name:CT_NOTI_PUBLISH_SUCCESS object:nil];
    }
    return self;
}



-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [discoveryView reloadData];

    //统计
    [MobClick beginLogPageView:@"CTDiscoveryViewController"];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //统计
    [MobClick endLogPageView:@"CTDiscoveryViewController"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    discoveryView = [[CTDiscoveryView alloc] init];
    discoveryView.delegate = self;
    [self.view addSubview:discoveryView];
    
    if ([CTDataGenerator generateFollowListItems] != nil || [CTDataGenerator generateFollowListItems].count !=0)
    discoveryView.followListArray = [CTDataGenerator generateFollowListItems];
    
    [discoveryView refresh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}


#pragma mark - request -


-(void)sendFollowListRequest
{
    [discoveryLogic followListRequest:[CTGenRequestData genRequestFollowListWithFilpNumber:discoveryFlipNumber] successCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            discoveryView.followListArray = [CTDataGenerator generateFollowListItems];
            [discoveryView refreshPullDownEnd];
        });
    } failedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([CTDataGenerator generateFollowListItems] != nil || [CTDataGenerator generateFollowListItems].count !=0)
                discoveryView.followListArray = [CTDataGenerator generateFollowListItems];
            [discoveryView refreshPullDownEnd];
        });
    }];
}

-(void)sendUserSuggestionRequest
{
    [discoveryLogic userSuggestionRequest:[CTGenRequestData genRequestUserSuggestionItem] successCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            discoveryView.suggestionUserListArray = [CTDataGenerator generateUserSuggestionItems];
            [discoveryView refreshPullDownEnd];
        });
    } failedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [discoveryView refreshPullDownEnd];
        });
    }];
}


#pragma mark - discoveryView delegate -




-(void)followDetailClicked:(CTDiscoveryView *)view withItem:(id)itm
{
    
}

-(void)refreshPullDown:(CTDiscoveryView *)view
{
    discoveryFlipNumber = 1;
    
    [self sendFollowListRequest];
    [self sendUserSuggestionRequest];
}
-(void)refreshPullUp:(CTDiscoveryView *)view
{
    discoveryFlipNumber ++;
    
    [self sendFollowListRequest];
}

/**
 *@brief 点击评论总数
 */
- (void)commentCountAction:(CTDiscoveryView *)view withItem:(CTFollowList *)item
{
    
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
        [tabbarVC hideTabbarWithAnimate:YES];
        
        CTCommentViewController * commentVC = [[CTCommentViewController alloc] init];
        commentVC.followListItem = item;
        commentVC.levelType = CTCommentViewControllerLevelsFirst;
        [self.navigationController pushViewController:commentVC animated:YES];
        
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}
/**
 *@brief 点击评论第一条
 */
- (void)commentHitFirstLabelAction:(CTDiscoveryView *)view withItem:(CTFollowList *)item
{
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
        [tabbarVC hideTabbarWithAnimate:YES];
        
        CTCommentViewController * commentVC = [[CTCommentViewController alloc] init];
        commentVC.followListItem = item;
        commentVC.levelType = CTCommentViewControllerLevelsFirst;
        [self.navigationController pushViewController:commentVC animated:YES];
        
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}
/**
 *@brief 点击评论第二条
 */
- (void)commentHitSecondLabelAction:(CTDiscoveryView *)view withItem:(CTFollowList *)item
{
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
        [tabbarVC hideTabbarWithAnimate:YES];
        
        CTCommentViewController * commentVC = [[CTCommentViewController alloc] init];
        commentVC.followListItem = item;
        commentVC.levelType = CTCommentViewControllerLevelsFirst;
        [self.navigationController pushViewController:commentVC animated:YES];
        
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}
/**
 *@brief 点击评论第一条头像
 */
- (void)commentHitFirstHeadAction:(CTDiscoveryView *)view withItem:(CTComment *)item
{
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
        [tabbarVC hideTabbarWithAnimate:YES];
        CTUserViewController * userVC = [[CTUserViewController alloc] init];
        userVC.pushType = CTUserViewControllerPushFromTypeOther;
        userVC.levelType = CTUserViewControllerLevelFirst;
        userVC.userUid = item.user.uid;
        [self.navigationController pushViewController:userVC animated:YES];
        
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}
/**
 *@brief 点击评论第二条头像
 */
- (void)commentHitSecondHeadAction:(CTDiscoveryView *)view withItem:(CTComment *)item
{
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        
        CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
        [tabbarVC hideTabbarWithAnimate:YES];
        CTUserViewController * userVC = [[CTUserViewController alloc] init];
        userVC.pushType = CTUserViewControllerPushFromTypeOther;
        userVC.levelType = CTUserViewControllerLevelFirst;
        userVC.userUid = item.user.uid;
        [self.navigationController pushViewController:userVC animated:YES];
        
        
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}
/**
 *@brief 点击头像
 */
- (void)hitHeadAction:(CTDiscoveryView *)view withItem:(CTFollowList *)item
{
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        
        CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
        [tabbarVC hideTabbarWithAnimate:YES];
        CTUserViewController * userVC = [[CTUserViewController alloc] init];
        userVC.pushType = CTUserViewControllerPushFromTypeOther;
        userVC.levelType = CTUserViewControllerLevelFirst;
        userVC.userUid = item.user.uid;
        [self.navigationController pushViewController:userVC animated:YES];
        
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}
/**
 *@brief 点击大图
 */
- (void)hitPicImageBtnAction:(CTDiscoveryView *)view withItem:(CTFollowList *)item withImageView:(UIImageView *)imageView
{
    if ([[CTTextManager shareInstance] isBlankString:item.pic_url]) return;
    
    NSMutableArray * photos = [NSMutableArray arrayWithObjects:0, nil];
    MJPhoto *photo = [[MJPhoto alloc] init];
    photo.image = [CTRequestUtil requestImageWithUrl:item.pic_url]; // 图片路径
    photo.srcImageView = imageView;
    [photos addObject:photo];
    
 //   if (photo.image == nil) return;
    // 2.显示相册
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.photos = photos; // 设置所有的图片
    [browser show];
    photos = nil;
    
}
/**
 *@brief 点赞
 */
- (void)hitFavourBtnAction:(CTDiscoveryView *)view withItem:(CTFollowList *)item withBool:(BOOL) favBool
{

    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        if (favBool == YES) {
            [logic likePersonRequest:[CTGenRequestData genRequestLikePseronItem:item.scheme_id] successCompletionHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [CTDebugManager showDebugLogInfo:@"点赞成功！"];
                  //  [[NSNotificationCenter defaultCenter] postNotificationName:CT_NOTI_FOLLOWING_SUCCESS object:nil];
                });
            } failedHandler:^{
                [CTDebugManager showErrorLogInfo:@"点赞失败！"];
            }];
        }else{
            [logic cancelLikePersonRequest:[CTGenRequestData genRequestCancelLikePseronItem:item.scheme_id] successCompletionHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [CTDebugManager showDebugLogInfo:@"取消点赞成功！"];
                  //  [[NSNotificationCenter defaultCenter] postNotificationName:CT_NOTI_CANCEL_FOLLOWING_SUCCESS object:nil];
                });
            } failedHandler:^{
                [CTDebugManager showErrorLogInfo:@"取消点赞失败！"];
            }];
        }
        
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}
/**
 *@brief 点击评论按钮
 */
- (void)hitCommentBtnaction:(CTDiscoveryView *)view withItem:(CTFollowList *)item
{
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
        [tabbarVC hideTabbarWithAnimate:YES];
        
        CTCommentViewController * commentVC = [[CTCommentViewController alloc] init];
        commentVC.followListItem = item;
        commentVC.levelType = CTCommentViewControllerLevelsFirst;
        [self.navigationController pushViewController:commentVC animated:YES];
        
        
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}
/**
 *@brief 点击分享按钮
 */
- (void)hitShareBtnAction:(CTDiscoveryView *)view withItem:(CTFollowList *)item withImage:(UIImage *)img
{
    shareImage = img;
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        currentFollowListItem = item;
        
        //share view
        shareView = [[CTShareView alloc] init];
        shareView.delegate = self;
        [shareView showShareViewOn:self.view.window withShareViewType:CTShareViewTypeDefault];
        
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}
/**
 *@brief 点击更多按钮
 */
- (void)hitMoreBtnAction:(CTDiscoveryView *)view withItem:(CTFollowList *)item
{
    currentFollowListItem = item;
    
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        CTUserItem * userItem = [CTUser userInfo];
        if ([item.user.uid isEqualToString:userItem.uid]) {
            CTMoreView * moreView = [[CTMoreView alloc] init];
            moreView.delegate = self;
            [moreView showMoreViewOn:self.view.window withMoreViewType:CTMoreViewTypeDefault];
        }else{
            CTMoreView * moreView = [[CTMoreView alloc] init];
            moreView.delegate = self;
            [moreView showMoreViewOn:self.view.window withMoreViewType:CTMoreViewTypeReport];
        }
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}
/**
 *@brief 点击点赞的人的头像
 */
- (void)hitFavBtnHeadAction:(CTDiscoveryView *)view withItem:(CTListDetailUser *)item
{
    
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        
        CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
        [tabbarVC hideTabbarWithAnimate:YES];
        CTUserViewController * userVC = [[CTUserViewController alloc] init];
        userVC.pushType = CTUserViewControllerPushFromTypeOther;
        userVC.levelType = CTUserViewControllerLevelFirst;
        userVC.userUid = item.uid;
        [self.navigationController pushViewController:userVC animated:YES];
        
        
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}
/**
 *@brief 点击关注按钮
 */
- (void)hitAttentionBtnAction:(CTDiscoveryView *)view withItem:(CTFollowList *)item withBool:(BOOL)attenBool
{
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        if (attenBool == YES) {
            [logic followPersonRequest:[CTGenRequestData genRequestFollowPseronItem:item.user.uid] successCompletionHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [CTDebugManager showDebugLogInfo:@"关注成功！"];
                  //  [[NSNotificationCenter defaultCenter] postNotificationName:CT_NOTI_FOLLOWING_SUCCESS object:nil];
                });
            } failedHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [CTDebugManager showErrorLogInfo:@"关注失败！"];
                });
            }];
        }else{
            [logic cancelFollowPersonRequest:[CTGenRequestData genRequestCancelFollowPseronItem:item.user.uid] successCompletionHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [CTDebugManager showDebugLogInfo:@"取消关注成功！"];
                  //  [[NSNotificationCenter defaultCenter] postNotificationName:CT_NOTI_CANCEL_FOLLOWING_SUCCESS object:nil];
                });
            } failedHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [CTDebugManager showErrorLogInfo:@"取消关注失败！"];
                });
            }];
        }
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}
/**
 *@brief 点击话题
 */
- (void)hitTopicBtnAction:(CTDiscoveryView *)view withItem:(CTTopic *)item
{
    CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
    [tabbarVC showTabbarWithAnimate:YES];
    [tabbarVC selectTabbarIndex:0];
    

    [[NSNotificationCenter defaultCenter] postNotificationName:CT_NOTI_JUMP_TO_GROUP object:item];
}


//推荐cell回调

/**
 *@brief 点击关注按钮
 */
- (void)recommendHitAttentionBtnAction:(CTDiscoveryView *) view withItem:(CTUserSuggestionItem *)item
{
    
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        [logic followPersonRequest:[CTGenRequestData genRequestFollowPseronItem:item.user.uid] successCompletionHandler:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [CTDebugManager showDebugLogInfo:@"关注成功！"];
                [[NSNotificationCenter defaultCenter] postNotificationName:CT_NOTI_FOLLOWING_SUCCESS object:nil];
                
                CTRemindView * remindView = [[CTRemindView alloc] init];
                [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"discoveryViewVC_Attention_Success" withValue:nil withTable:@"Language"]];
            });
        } failedHandler:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [CTDebugManager showErrorLogInfo:@"关注失败！"];
                [[NSNotificationCenter defaultCenter] postNotificationName:CT_NOTI_FOLLOWING_Failed object:nil];
                
                CTRemindView * remindView = [[CTRemindView alloc] init];
                [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"discoveryViewVC_Attention_Failed" withValue:nil withTable:@"Language"]];
            });
        }];
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
    
}
/**
 *@brief 点击头像
 */
- (void)recommendHitHeadAction:(CTDiscoveryView *)view withItem:(CTUserSuggestionItem *)item
{
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        
        CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
        [tabbarVC hideTabbarWithAnimate:YES];
        CTUserViewController * userVC = [[CTUserViewController alloc] init];
        userVC.pushType = CTUserViewControllerPushFromTypeOther;
        userVC.levelType = CTUserViewControllerLevelFirst;
        userVC.userUid = item.user.uid;
        [self.navigationController pushViewController:userVC animated:YES];
        
        
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}













#pragma mark - share view delegate -

//试图移除
-(void)viewDidRemoved:(CTShareView *)view
{
    shareView = nil;
    shareView.delegate = nil;
}
//分享到微信朋友圈
- (void)shareViewShareToWechatFriends:(CTShareView *) view withImage:(UIImage *)image
{
    CTShareItem * shareItem = shareItem = [[CTShareItem alloc] init];
    shareItem.img = shareImage;//[CTRequestUtil requestImageWithUrl:currentFollowListItem.pic_url];
    shareItem.content = currentFollowListItem.text;
    shareItem.defaultContent = currentFollowListItem.text;
    shareItem.url   = currentFollowListItem.share_url;
    shareItem.descr = [CTAppLanguage localizedStringForKey:@"share_topic_describe" withValue:nil withTable:@"Language"];;
    shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
    
    
    [CTShare shareItem:shareItem shareChannel:CTShareChannelWechatFriend publishType:CTPublishTypeNews shareSuccessHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });
    } shareFailedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });
    }];
}
//分享到QQ空间
- (void)shareViewShareToQQSpace:(CTShareView *)view withImage:(UIImage *)image
{
    CTShareItem * shareItem = shareItem = [[CTShareItem alloc] init];
    UIImage *img = shareImage;//[CTRequestUtil requestImageWithUrl:currentFollowListItem.pic_url];
    CGSize size = CGSizeMake(320, 320);
    shareItem.img = [CTImageProcessing imageCompressionImg:img WithSize:size];
    shareItem.content = currentFollowListItem.text;
    shareItem.defaultContent = currentFollowListItem.text;
    shareItem.url = currentFollowListItem.share_url;
    shareItem.descr = [CTAppLanguage localizedStringForKey:@"share_pic_describe" withValue:nil withTable:@"Language"];
    shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
    
    [CTShare shareItem:shareItem shareChannel:CTShareChannelQQSpace publishType:CTPublishTypeImage shareSuccessHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });
    } shareFailedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });
    }];
}
//分享到新浪微博
- (void)shareViewShareToSina:(CTShareView *)view withImage:(UIImage *)image
{
    CTShareItem * shareItem = shareItem = [[CTShareItem alloc] init];
    shareItem.img = shareImage;//[CTRequestUtil requestImageWithUrl:currentFollowListItem.pic_url];
    shareItem.content = currentFollowListItem.share_url;
    shareItem.defaultContent = currentFollowListItem.text;
    shareItem.url = currentFollowListItem.share_url;
    shareItem.descr = [CTAppLanguage localizedStringForKey:@"share_pic_describe" withValue:nil withTable:@"Language"];;
    shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
    
//    NSLog(@"%@\n %@\n %@\n %@\n %@\n %@\n",shareItem.img,shareItem.content,shareItem.defaultContent,shareItem.url,shareItem.descr,shareItem.title);
    [CTShare sinaBtnAction:shareItem  shareSuccessHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });
    } shareFailedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });
    }];
}
//分享到微信
- (void)shareViewShareToWeChat:(CTShareView *)view withImage:(UIImage *)image
{
    CTShareItem * shareItem = shareItem = [[CTShareItem alloc] init];
    shareItem.img = shareImage;//[CTRequestUtil requestImageWithUrl:currentFollowListItem.pic_url];
    shareItem.defaultContent = currentFollowListItem.text;
    shareItem.descr = currentFollowListItem.text;
    shareItem.url = currentFollowListItem.share_url;
    shareItem.content = [CTAppLanguage localizedStringForKey:@"share_pic_describe" withValue:nil withTable:@"Language"];
    shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
    
    [CTShare shareItem:shareItem shareChannel:CTShareChannelWechat publishType:CTPublishTypeNews shareSuccessHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });
    } shareFailedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });
    }];
}
//分享到QQ
- (void)shareViewShareToQQ:(CTShareView *)view withImage:(UIImage *)image
{
    
    CTShareItem * shareItem = shareItem = [[CTShareItem alloc] init];
    
    UIImage *img = shareImage;//[CTRequestUtil requestImageWithUrl:currentFollowListItem.pic_url];
    CGSize size = CGSizeMake(320, 320);
    shareItem.img =  [CTImageProcessing imageCompressionImg:img WithSize:size];
    shareItem.defaultContent = currentFollowListItem.text;
    shareItem.descr = currentFollowListItem.text;
    shareItem.url =  currentFollowListItem.share_url;
    shareItem.content = [CTAppLanguage localizedStringForKey:@"share_pic_describe" withValue:nil withTable:@"Language"];
    shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
    
    [CTShare shareItem:shareItem shareChannel:CTShareChannelQQ publishType:CTPublishTypeImage shareSuccessHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });
    } shareFailedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });
    }];
}






#pragma mark - more view delegate -

- (void)moreViewDelBtnAction:(CTMoreView *)view
{
    reportType = ReportTypeDelete;
    
    NSString * titleStr = [CTAppLanguage localizedStringForKey:@"CTHomePageViewController_Alert_Remind" withValue:nil withTable:@"Language"];
    NSString * messageStr = [CTAppLanguage localizedStringForKey:@"CTHomePageViewController_Alert_Message_Delete" withValue:nil withTable:@"Language"];
    NSString * confirmStr = [CTAppLanguage localizedStringForKey:@"CTHomePageViewController_Alert_Confirm" withValue:nil withTable:@"Language"];
    NSString * cancelStr = [CTAppLanguage localizedStringForKey:@"CTHomePageViewController_Alert_Cancel" withValue:nil withTable:@"Language"];
    
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:titleStr message:messageStr delegate:self cancelButtonTitle:cancelStr otherButtonTitles:confirmStr, nil];
    [alertView show];
    
    view = nil;
}
- (void)moreViewreportBtnAction:(CTMoreView *)view
{
    reportType = ReportTypeReport;
    
    NSString * titleStr = [CTAppLanguage localizedStringForKey:@"CTHomePageViewController_Alert_Remind" withValue:nil withTable:@"Language"];
    NSString * messageStr = [CTAppLanguage localizedStringForKey:@"CTHomePageViewController_Alert_Message_Report" withValue:nil withTable:@"Language"];
    NSString * confirmStr = [CTAppLanguage localizedStringForKey:@"CTHomePageViewController_Alert_Confirm" withValue:nil withTable:@"Language"];
    NSString * cancelStr = [CTAppLanguage localizedStringForKey:@"CTHomePageViewController_Alert_Cancel" withValue:nil withTable:@"Language"];
    
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:titleStr message:messageStr delegate:self cancelButtonTitle:cancelStr otherButtonTitles:confirmStr, nil];
    [alertView show];
    
    view = nil;
}
- (void)moreViewCanacelBtnAction:(CTMoreView *)view
{
    
    view = nil;
}
- (void)moreViewDidRemoved:(CTMoreView *)view
{
    view = nil;
}

#pragma mark - alert view delegate -

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex == 1) {
        if (reportType == ReportTypeReport) {
            //举报
            acView = [[CTActivitorView alloc] init];
            [acView showActivitorViewOn:self.view withType:CTActivitorViewDefault];
            
            [logic reportRequest:[CTGenRequestData genRequestReportItem:currentFollowListItem.scheme_id] successCompletionHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [acView removeActivitorView];
                    acView = nil;
                    
                    CTRemindView * remindView = [[CTRemindView alloc] init];
                    [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"CTHomePageViewController_Report_Success" withValue:nil withTable:@"Language"]];
                    
                });
            } failedHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [acView removeActivitorView];
                    acView = nil;
                    
                    CTRemindView * remindView = [[CTRemindView alloc] init];
                    [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"CTHomePageViewController_Report_Failed" withValue:nil withTable:@"Language"]];
                    
                });
            }];
        }else{
            //删除自己的发布内容
            
            acView = [[CTActivitorView alloc] init];
            [acView showActivitorViewOn:self.view withType:CTActivitorViewDefault];
            
            [logic deletePublishRequest:[CTGenRequestData genRequestDeletePublishItem:currentFollowListItem.scheme_id] successCompletionHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [[NSNotificationCenter defaultCenter] postNotificationName:CT_NOTI_DELETE_PUBLISH_SUCCESS object:nil];
                    
                    [acView removeActivitorView];
                    acView = nil;
                    
                    CTRemindView * remindView = [[CTRemindView alloc] init];
                    [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"CTHomePageViewController_Delete_Success" withValue:nil withTable:@"Language"]];
                    
                });
            } failedHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    
                    [acView removeActivitorView];
                    acView = nil;
                    
                    CTRemindView * remindView = [[CTRemindView alloc] init];
                    [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"CTHomePageViewController_Delete_Failed" withValue:nil withTable:@"Language"]];
                    
                });
            }];
            
        }
    }
    
    alertView.delegate = nil;
    [alertView removeFromSuperview];
    alertView = nil;
}






#pragma mark - login & exit -

-(void)loginSuccess:(id)sender
{
    [discoveryView refresh];
}
-(void)exitSuccess:(id)sender
{
    [discoveryView refresh];
}

-(void)followPersonSuccess:(id)sender
{
   // [discoveryView refresh];
    discoveryFlipNumber = 1;
    [self sendFollowListRequest];
    [self sendUserSuggestionRequest];
}

-(void)followPersonFailed:(id)sender
{
    // [discoveryView refresh];
    discoveryFlipNumber = 1;
    [self sendFollowListRequest];
    [self sendUserSuggestionRequest];
}

-(void)cancelFollowPersonSuccess:(id)sender
{
    // [discoveryView refresh];
    discoveryFlipNumber = 1;
    [self sendFollowListRequest];
    [self sendUserSuggestionRequest];
}

-(void)likePersonSuccess:(id)sender
{
    // [discoveryView refresh];
    discoveryFlipNumber = 1;
    [self sendFollowListRequest];
    [self sendUserSuggestionRequest];
}

-(void)cancelLikePersonSuccess:(id)sender
{
    // [discoveryView refresh];
    discoveryFlipNumber = 1;
    [self sendFollowListRequest];
    [self sendUserSuggestionRequest];
}

-(void)publishCommentSuccessed:(id)sender
{
    // [discoveryView refresh];
    discoveryFlipNumber = 1;
    [self sendFollowListRequest];
    [self sendUserSuggestionRequest];
}

-(void)deleteCommentSuccessed:(id)sender
{
    // [discoveryView refresh];
    discoveryFlipNumber = 1;
    [self sendFollowListRequest];
    [self sendUserSuggestionRequest];
}

-(void)deletePublishSuccessed:(id)sender
{
    // [discoveryView refresh];
    discoveryFlipNumber = 1;
    [self sendFollowListRequest];
    [self sendUserSuggestionRequest];
}

-(void)publishSuccess:(id)sender
{
    [discoveryView refresh];
}

@end
